package com.genreate.util.douyin;

import cn.hutool.core.collection.IterUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * 抖音工具类
 *
 * @author LYS
 * @date 2025-06-07
 */
public class DouYinUtil {
    /**
     * 获取表情包图片连接
     *
     * @param keyword 关键词
     * @param cookie  cookie
     * @return {@link List }<{@link String }>
     */
    public static List<String> getEmoj(String keyword, String cookie){
        String url = StrUtil.format("https://www.douyin.com/aweme/v1/web/im/resource/emoticon/search?keyword={}",keyword);
        String body = HttpRequest.get(url)
                .header("Cookie", "")
                .header("referer", "https://www.douyin.com/jingxuan?enter=guide")
                .execute()
                .body();
        JSONObject obj = JSONUtil.parseObj(body);
        JSONArray jsonArray = obj.getJSONObject("emoticon_data").getJSONArray("sticker_list");
        List<String> urlList = new ArrayList<>();
        for (Object o : jsonArray) {
            JSONArray emoList = JSONUtil.parseObj(o).getJSONObject("origin").getJSONArray("url_list");
            if(IterUtil.isNotEmpty(emoList)){
                String urlEmo = Convert.toStr(emoList.get(0));
                urlList.add(urlEmo);
            }
        }
        return urlList;
    }
}
